package com.huang.community.Entity;

import com.alibaba.fastjson.JSONObject;
import com.huang.community.common.CommunityConstant;
import com.huang.community.pojo.DiscussPost;
import com.huang.community.pojo.Message;
import com.huang.community.service.DiscussPostService;
import com.huang.community.service.ElasticsearchService;
import com.huang.community.service.MessageService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;

import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 消息队列消费者
 * @author TianTian
 * @date 2022/4/28 22:13
 */
@Slf4j
@Component
public class EventConsumer implements CommunityConstant {
    @Autowired
    private DiscussPostService discussPostService;
    @Autowired
    private RestHighLevelClient restHighLevelClient;
    @Autowired
    private ElasticsearchService elasticsearchService;
    @Autowired
    private MessageService messageService;
    //follow队列
    @RabbitListener(queues = "QA")
    public void receiveLike(String jsonString, Channel channel){
      if(  jsonString==null||jsonString.length()==0){
          log.error("消息为空");
      }
        Event event = JSONObject.parseObject(jsonString, Event.class);
      if (event==null){
          log.error("类型转换失败");
      }
    sendMessage(event);
    }
    //Comment队列
    @RabbitListener(queues = "QB")
    public void receiveComment(String jsonString, Channel channel){
        if(  jsonString==null||jsonString.length()==0){
            log.error("消息为空");
        }
        Event event = JSONObject.parseObject(jsonString, Event.class);
        if (event==null){
            log.error("类型转换失败");
        }
        sendMessage(event);

    }
    //like队列
    @RabbitListener(queues = "QC")
    public void receiveFollow(String jsonString, Channel channel){
        if(  jsonString==null||jsonString.length()==0){
            log.error("消息为空");
        }
        Event event = JSONObject.parseObject(jsonString, Event.class);
        if (event==null){
            log.error("类型转换失败");
        }
        sendMessage(event);
    }
    //pubilsh队列
    @RabbitListener(queues = "QD")
    public void receivePublish(String jsonString, Channel channel){
        if(  jsonString==null||jsonString.length()==0){
            log.error("消息为空");
        }
        Event event = JSONObject.parseObject(jsonString, Event.class);
        if (event==null){
            log.error("类型转换失败");
            return;
        }
        DiscussPost post = discussPostService.getDiscussPostById(event.getEntityId());
        elasticsearchService.saveDiscussPost(post);

    }
    //delete队列
    @RabbitListener(queues = "QE")
    public void receiveDelete(String jsonString, Channel channel) {
        if (jsonString == null || jsonString.length() == 0) {
            log.error("消息为空");
        }
        Event event = JSONObject.parseObject(jsonString, Event.class);
        if (event == null) {
            log.error("类型转换失败");
            return;
        }
        elasticsearchService.deleteDiscussPost(event.getEntityId());
    }


    //发送
    public void sendMessage(Event event){
    // 发送站内通知
    Message message = new Message();
        message.setFromId(SYSTEM_USER_ID);
        message.setToId(event.getEntityUserId());
        message.setConversationId(event.getTopic());
        message.setCreateTime(new Date());

    Map<String, Object> content = new HashMap<>();
        content.put("userId", event.getUserId());
        content.put("entityType", event.getEntityType());
        content.put("entityId", event.getEntityId());

        if (!event.getData().isEmpty()) {
        for (Map.Entry<String, Object> entry : event.getData().entrySet()) {
            content.put(entry.getKey(), entry.getValue());
        }
    }
        message.setContent(JSONObject.toJSONString(content));
        messageService.addMessage(message);
}

}
